package model

import (
	"dag2.0/gat1400-proxy/logger"
	"dag2.0/gat1400-proxy/util"
	"dag2.0/gat1400-proxy/util/times"
	"fmt"
	jsoniter "github.com/json-iterator/go"
	"time"
)

const (
	OK                      = "0"
	OTHER_ERROR             = "1"
	DEVICE_BUSY             = "2"
	DEVICE_ERROR            = "3"
	INVALID_OPERATION       = "4"
	XML_FORMAT_INVALID      = "5"
	XML_CONTENT_INVALID     = "6"
	JSON_FORMAT_INVALID     = "7"
	JSON_CONTENT_INVALID    = "8"
	SYSTEM_REBOOT           = "9"
	VIEWID_IS_NULL          = "10"
	PASSWORD_IS_NULL        = "11"
	RESPONSE_NOT_CORRECT    = "12"
	AUTHORIZATION_IS_NULL   = "13"
	KEEPALIVEOBJECT_IS_NULL = "14"
	DEVICEID_IS_NOT_EXIST   = "15"
	UAERNAEM_NOT_CORRECT    = "16"
	UNAUTHORIZED            = "17"
	RESULT_IS_NULL          = "18"
	PARAM_IS_EMPTY          = "19"
	SUBCRIBEID_IS_NULL      = "20"
)

var statusText = map[string]string{
	OK:                      "正常",
	OTHER_ERROR:             "其他未知错误",
	DEVICE_BUSY:             "设备忙",
	DEVICE_ERROR:            "设备错误",
	INVALID_OPERATION:       "无效操作",
	XML_FORMAT_INVALID:      "XML格式无效",
	XML_CONTENT_INVALID:     "XML内容无效",
	JSON_FORMAT_INVALID:     "JSON格式无效",
	JSON_CONTENT_INVALID:    "JSON内容无效",
	SYSTEM_REBOOT:           "系统重启中",
	VIEWID_IS_NULL:          "视图库id为空",
	PASSWORD_IS_NULL:        "密码为空",
	RESPONSE_NOT_CORRECT:    "response 不正确",
	AUTHORIZATION_IS_NULL:   "Authorization 不能为空",
	KEEPALIVEOBJECT_IS_NULL: "keepalive 对象不能为空",
	DEVICEID_IS_NOT_EXIST:   "ManagerID is not exist",
	UAERNAEM_NOT_CORRECT:    "注册时用户名或密码不正确",
	UNAUTHORIZED:            "Authentication failure,Please to register",
	RESULT_IS_NULL:          "查询结果为空",
	PARAM_IS_EMPTY:          "参数为空",
	SUBCRIBEID_IS_NULL:      "订阅id为空",
}

type Response struct {
	ResponseStatusListObject *ResponseStatusListObject `json:"ResponseStatusListObject"`
}

type ResponseOth struct {
	ResponseStatusListObject *ResponseStatusListObject `json:"ResponseStatusListObject"`
}
type ResponseStatusListObject struct {
	ResponseStatusObject []*ResponseStatusObject `json:"ResponseStatusObject"`
}
type ResponseStatusSingleObj struct {
	ResponseStatusObject *ResponseStatusObject `json:"ResponseStatusObject"`
}

type ResourceResponse struct {
	RosourceID   string      `json:"RosourceID,omitempty"`
	RequestURI   string      `json:"RequestURI"`
	StatusCode   string      `json:"code"`
	StatusString string      `json:"status"`
	Data         interface{} `json:"data,omitempty"`
}

type ResponseStatusObject struct {
	ID           string `json:"Id"`
	LocalTime    string `json:"LocalTime"`
	RequestURL   string `json:"RequestURL"`
	StatusCode   string `json:"StatusCode"`
	StatusString string `json:"StatusString"`
}

func BuildRespnseObject(url string, recordId string, code string) *ResponseStatusObject {
	return &ResponseStatusObject{
		ID:           recordId,
		LocalTime:    times.Time2StrF(time.Now(), "20060102150405"),
		RequestURL:   url,
		StatusCode:   code,
		StatusString: statusText[code],
	}
}

func BuildResponse(objs ...*ResponseStatusObject) *Response {
	return &Response{
		ResponseStatusListObject: &ResponseStatusListObject{
			ResponseStatusObject: objs,
		},
	}
}
func BuildResponseList(objs *ResponseStatusListObject) *Response {
	return &Response{
		ResponseStatusListObject: objs,
	}
}

func BuildSingleResponse(obj *ResponseStatusObject) *ResponseStatusSingleObj {
	return &ResponseStatusSingleObj{
		ResponseStatusObject: obj,
	}
}

func BuildResourceResponse(code string, url string, id string, data interface{}) *ResourceResponse {
	return &ResourceResponse{
		RosourceID:   id,
		RequestURI:   url,
		StatusCode:   code,
		StatusString: statusText[code],
		Data:         data,
	}
}

func AnalyzeRsponseContent(body []byte) *Response {
	// 批量对象返回
	response := &Response{}
	err := jsoniter.Unmarshal(body, response)
	if nil == err && response.ResponseStatusListObject != nil && len(response.ResponseStatusListObject.ResponseStatusObject) > 0 {
		return response
	}
	logger.LOG_INFO("返回数据为 ：", util.BytesString(body))
	fmt.Println(util.BytesString(body))
	if util.BytesString(body) == "true" {
		return BuildResponse(BuildRespnseObject("/VIID/SubscribeNotifications", "", "0"))
	}
	// 单个对象返回
	responseStatusSingleObject := &ResponseStatusSingleObj{}
	err = jsoniter.Unmarshal(body, responseStatusSingleObject)
	if nil == err && responseStatusSingleObject.ResponseStatusObject != nil {
		return BuildResponse(responseStatusSingleObject.ResponseStatusObject)
	}
	//一所返回
	responseStatusObject := &ResponseStatusObject{}
	err = jsoniter.Unmarshal(body, responseStatusObject)
	if nil == err && response.ResponseStatusListObject != nil {
		return BuildResponse(responseStatusObject)
	}
	return nil
}
